package com.bocai.controller;

import com.bocai.bean.CardInfo;
import com.bocai.service.ICardService;
import com.bocai.utils.CodeEnum;
import com.bocai.utils.ObjUtils;
import com.bocai.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
@RequestMapping("CardInfo")
public class CardInfoController {
    @Autowired
    private ICardService cardService;


    @ApiOperation(value = "根据金额查询代金券", notes = "根据金额查询代金券")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cardMoney", required = true, dataType = "long", value = "充值金额", defaultValue = "100"),
    })
    @RequestMapping(value = "/findCardInfoMoney", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findCardInfo(BigDecimal cardMoney) {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.SUCCESS);
        data.setResults(cardService.selectCardInfo(cardMoney));
        return data;
    }

    @ApiOperation(value = "查询代金券", notes = "查询代金券")
    @RequestMapping(value = "/findCardInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData findCardInfo() {
        ResponseData data = new ResponseData();
        data.setCodeEnum(CodeEnum.SUCCESS);
        CardInfo cardInfo = cardService.selectCardInfo();
        if (null == cardInfo) {
            data.setCodeEnum(CodeEnum.CODE_60009);
        } else {
            data.setResults(cardInfo);
        }
        return data;
    }


    @ApiOperation(value = "修改代金券是否下架 或者 添加", notes = "修改代金券是否下架  或者 添加")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cardMoney", required = true, dataType = "long", value = "充值金额", defaultValue = "100"),
            @ApiImplicitParam(paramType = "query", name = "cardRewardMoney", required = true, dataType = "long", value = "充值金额", defaultValue = "100"),
            @ApiImplicitParam(paramType = "query", name = "cardState", required = true, dataType = "int", value = "是否启用（1启用0不启用）", defaultValue = "1"),
    })
    @RequestMapping(value = "/updateCardInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData updateCardInfo(String cardMoney, String cardRewardMoney, Integer cardState) {
        ResponseData data = new ResponseData();
        try {
            BigDecimal bigDecimal = new BigDecimal(cardMoney);
            BigDecimal bigDecimal2 = new BigDecimal(cardRewardMoney);
            CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{cardState});
            if (ObjUtils.isNotEmpty(codeEnum)) {
                data.setCodeEnum(codeEnum);
                return data;
            }
            data.setCodeEnum(CodeEnum.SUCCESS);
            if (cardService.updateCardInfo(bigDecimal, bigDecimal2, cardState) == 0) {
                data.setCodeEnum(CodeEnum.CODE_60009);
            }
        } catch (Exception e) {
            data.setCodeEnum(CodeEnum.CODE_10010);
        }
        return data;
    }
}
